/**
 * 
 */
package org.pub.lunch.google.map.view;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import org.pub.app.lunch.R;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;


/**
 * @author Austin Cho
 *
 */
public class SelectedRestaurantMap extends MapActivity {

	private static final String TAG = "SelectedRestaurantMap";
	private MapView map = null;
	private MyLocationOverlay me = null;
	private MyLocation resLoc = null;
	private MyLocation myLoc = null;
	
	public MyLocation getResLoc() {
		return resLoc;
	}

	public void setResLoc(MyLocation resLoc) {
		this.resLoc = resLoc;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		map = (MapView)findViewById(R.id.map);
		
		map.setBuiltInZoomControls(true);
		map.getController().setZoom(18);
		
		Location rest = (Location) getIntent().getExtras().getParcelable("RESTAURANT");
		String name = getIntent().getStringExtra("RESTAURANT_NAME");
		Location current = (Location)getIntent().getExtras().getParcelable("CURRENT");
		Geocoder gc = new Geocoder(this, Locale.KOREA);
		Address currentAddr = null;
		try {
			List<Address> currentLocation = gc.getFromLocation(current.getLatitude(), current.getLongitude(), 1);
			currentAddr = currentLocation.get(0);
			StringBuffer currentInfo = new StringBuffer();
			currentInfo.append(currentAddr.getLocality() + " " + currentAddr.getThoroughfare() + " " + currentAddr.getFeatureName());
			Log.i(TAG, currentInfo.toString());
			myLoc = new MyLocation(current.getLatitude(), current.getLongitude(), currentInfo.toString());
			resLoc = new MyLocation(rest.getLatitude(), rest.getLongitude(), name);
		} catch (IOException e) {
			Log.w(TAG, e);
			e.printStackTrace();
		}
		
		if ( myLoc == null || resLoc == null ) {
			Toast.makeText(this, R.string.INTERNET_ERROR, Toast.LENGTH_LONG);
			this.finish();
		}
		
		map.getController().setCenter(resLoc.getLocation());
		
//		Drawable marker = getResources().getDrawable(R.drawable.marker);
//		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
//		map.getOverlays().add(new RestaurantOverlay(mappin, this, myLoc, resLoc));
		
		Drawable mappin = getResources().getDrawable(R.drawable.mappin);
		Drawable currentMarker = getResources().getDrawable(R.drawable.my);
		currentMarker.setBounds(0, 0, currentMarker.getIntrinsicWidth(), currentMarker.getIntrinsicHeight());
		mappin.setBounds(0, 0, mappin.getIntrinsicWidth(), mappin.getIntrinsicHeight());
		
		map.getOverlays().add(new RestaurantOverlay(currentMarker, this, myLoc));
		map.getOverlays().add(new RestaurantOverlay(mappin, this, resLoc));
		
		me = new MyLocationOverlay(this, map);
		map.getOverlays().add(me);
	}
	
	@Override
	public void onResume() 
	{
		super.onResume();		
		me.enableCompass();
	}		
	
	@Override
	public void onPause() 
	{
		super.onPause();		
		me.disableCompass();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if ( keyCode == KeyEvent.KEYCODE_S) {
			map.setSatellite(!map.isSatellite());
			return true;
		}
		else if ( keyCode == KeyEvent.KEYCODE_Z ) {
			map.displayZoomControls(true);
			return true;
		}
		
		return (super.onKeyDown(keyCode, event));		
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
